package com.iris.netty.review;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import sun.misc.Unsafe;

public class NettyServer2 {
    Unsafe unsafe = Unsafe.getUnsafe();
    public static void main(String[] args) throws InterruptedException {
        ServerBootstrap serverBootstrap  =new ServerBootstrap();
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();
        serverBootstrap.group(boss, worker).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new StringDecoder())
                                .addLast(new StringEncoder())
                                .addLast(new NettyServerHandler());
                    }
                }).option(ChannelOption.SO_BACKLOG,1024)
        .childOption(ChannelOption.SO_KEEPALIVE,true)
        .childOption(ChannelOption.TCP_NODELAY,true);
        ChannelFuture future = serverBootstrap.bind(8889).sync();
        future.channel().closeFuture().sync();
//        Proxy.newpro
    }
}
